home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / mawk10.zip / MEMORY.C < prev    next >
C/C++ Source or Header  |  1991-10-05  |  3KB  |  107 lines

  1.  
  2. /********************************************
  3. memory.c
  4. copyright 1991, Michael D. Brennan
  5.  
  6. This is a source file for mawk, an implementation of
  7. the AWK programming language.
  8.  
  9. Mawk is distributed without warranty under the terms of
  10. the GNU General Public License, version 2, 1991.
  11. ********************************************/
  12.  
  13.  
  14. /* $Log:    memory.c,v $
  15.  * Revision 3.3.1.1  91/09/14  17:23:49  brennan
  16.  * VERSION 1.0
  17.  * 
  18.  * Revision 3.3  91/08/13  06:51:49  brennan
  19.  * VERSION .9994
  20.  * 
  21.  * Revision 3.2  91/06/28  04:17:06  brennan
  22.  * VERSION 0.999
  23.  * 
  24.  * Revision 3.1  91/06/07  10:27:56  brennan
  25.  * VERSION 0.995
  26.  * 
  27.  * Revision 2.2  91/06/03  07:54:08  brennan
  28.  * changed #ifdef __TURBOC__  to #if HAVE_PROTOS
  29.  * 
  30.  * Revision 2.1  91/04/08  08:23:35  brennan
  31.  * VERSION 0.97
  32.  * 
  33. */
  34.  
  35.  
  36. /* memory.c */
  37.  
  38. #include "mawk.h"
  39.  
  40. #if     HAVE_PROTOS
  41. #define SUPPRESS_NEW_STRING_PROTO  /* get compiler off our back on
  42.          the definition of new_STRING() */
  43. #endif
  44.  
  45. #include "memory.h"
  46.  
  47. STRING null_str = {1, 0, "" } ;
  48.  
  49. static STRING *char_string[127] ;
  50. /* slots for strings of one character
  51.    "\01" thru "\177"    */
  52.   
  53. STRING *new_STRING(s, xlen)   
  54.   char *s ;  unsigned xlen ;
  55.   /* WARNING: if s != NULL, don't access xlen
  56.      because it won't be there   */
  57. { register STRING *p ;
  58.   unsigned len ;
  59.  
  60.   if ( s )
  61.         switch( len = strlen(s) )
  62.         {
  63.             case 0 : 
  64.                 p = &null_str  ; p->ref_cnt++ ;
  65.                 break ;
  66.  
  67.             case 1 :
  68.                 if ( *(unsigned char *)s < 128 )
  69.                 {   if ( p = char_string[*s-1] )
  70.                         p->ref_cnt++ ;
  71.                     else
  72.                     { p = (STRING *) zmalloc(6) ;
  73.                       p->ref_cnt = 2 ;  p->len = 1 ; 
  74.                       p->str[0] = s[0] ;
  75.                       p->str[1] = 0 ;
  76.                       char_string[*s-1] = p ;
  77.                     }
  78.  
  79.                     break ; /*case */
  80.                 }
  81.                 /* else FALL THRU */
  82.  
  83.             default :
  84.                 p = (STRING *) zmalloc(len+5) ;
  85.                 p->ref_cnt = 1 ; p->len = len ;
  86.                 (void) memcpy( p->str , s, SIZE_T(len+1)) ;
  87.                 break ;
  88.         }
  89.   else  
  90.   { p = (STRING *) zmalloc( xlen+5 ) ;
  91.     p->ref_cnt = 1 ; p->len = xlen ;
  92.     /* zero out the end marker */
  93.     p->str[xlen] = 0 ; 
  94.   }
  95.  
  96.   return p ;
  97. }
  98.  
  99.  
  100. #ifdef   DEBUG
  101.  
  102. void  DB_free_STRING(sval)
  103.   register STRING *sval ;
  104. { if ( -- sval->ref_cnt == 0 )  zfree(sval, sval->len+5) ; }
  105.  
  106. #endif
  107.